package nc.impl.dm.m4804.rule;

import java.util.HashMap;
import java.util.Map;
import nc.bs.ml.NCLangResOnserver;
import nc.impl.pubapp.pattern.rule.IRule;
import nc.vo.dm.m4804.entity.DelivBillAggVO;
import nc.vo.dm.m4804.entity.DelivBillBVO;
import nc.vo.dm.m4804.entity.DelivBillHVO;
import nc.vo.dm.m4804trantype.entity.M4804TranTypeVO;
import nc.vo.ml.NCLangRes4VoTransl;
import nc.vo.pub.lang.UFBoolean;
import nc.vo.pubapp.pattern.exception.ExceptionUtils;
import nc.vo.pubapp.pattern.pub.PubAppTool;











public class TrantypeValidation implements IRule<DelivBillAggVO> {
  private Map<String, M4804TranTypeVO> tranTypeMap = new HashMap<>();







  public TrantypeValidation(Map<String, M4804TranTypeVO> tranTypeMap) {
    this.tranTypeMap = tranTypeMap;
  }


  public void process(DelivBillAggVO[] vos) {
    try {
      for (DelivBillAggVO vo : vos) {
        checkTranType(vo);
        checkBadvfeeflag(vo);
        checkbodysettlefinorg(vo);
      } 
    } catch (Exception e) {
      ExceptionUtils.wrappException(e);
    } 
  }





  public void setTranTypeMap(Map<String, M4804TranTypeVO> tranTypeMap) {
    this.tranTypeMap = tranTypeMap;
  }

  private void checkTranType(DelivBillAggVO vo) {
    DelivBillHVO head = vo.getParentVO();

    boolean addManual = false;

    DelivBillBVO[] bodys = vo.getDelivBillBVO();
    if (bodys != null && bodys.length > 0)
      for (DelivBillBVO body : bodys) {
        if (PubAppTool.isNull(body.getCfirstid())) {
          addManual = true;


          break;
        } 
      }  
    boolean hasBodyBill = (vo.getDelivBillBVO() != null && (vo.getDelivBillBVO()).length > 0);
    boolean hasBodyPack = (vo.getDelivBillPackVO() != null && (vo.getDelivBillPackVO()).length > 0);
    if (!hasBodyBill && hasBodyPack)
      addManual = true; 


    if (addManual) {
      String ctrantypeCode = head.getVtrantypecode();
      M4804TranTypeVO tranType = this.tranTypeMap.get(ctrantypeCode);

      if (tranType.getBcanselfmadeflag() != null && tranType
        .getBcanselfmadeflag() == UFBoolean.FALSE) {
        String message = NCLangRes4VoTransl.getNCLangRes().getStrByID("4014001_0", "04014001-0343");

        ExceptionUtils.wrappBusinessException(message);
      } 
    } 
  }







  private void checkBadvfeeflag(DelivBillAggVO vo) {
    DelivBillHVO head = vo.getParentVO();
    String ctrantypeCode = head.getVtrantypecode();
    M4804TranTypeVO tranType = this.tranTypeMap.get(ctrantypeCode);
    if (tranType.getBcalculateapfeeflag().booleanValue() && head.getBadvfeeflag().booleanValue()) {
      String msg = NCLangResOnserver.getInstance().getStrByID("4014001_0", "04014001-0104");

      ExceptionUtils.wrappBusinessException(msg);
    } 
  }







  private void checkbodysettlefinorg(DelivBillAggVO vo) {
    DelivBillHVO head = vo.getParentVO();
    String ctrantypeCode = head.getVtrantypecode();
    M4804TranTypeVO tranType = this.tranTypeMap.get(ctrantypeCode);
    if (tranType.getBcalculateapfeeflag().booleanValue())
      for (DelivBillBVO bvo : vo.getDelivBillBVO()) {
        if (bvo.getStatus() != 3) {


          String csettlefinorgid = bvo.getCsettlefinorgid();
          if (csettlefinorgid == null) {
            String msg = NCLangResOnserver.getInstance().getStrByID("4014001_0", "04014001-0105");

            ExceptionUtils.wrappBusinessException(msg);
          } 
        } 
      }  
  }
}

